package com.gomokult.service.impl;

import com.gomokult.dao.UserDAO;
import com.gomokult.dao.impl.UserDAOImpl;
import com.gomokult.entity.User;
import com.gomokult.exception.IllegalOperationException;
import com.gomokult.exception.InvalidIDException;
import com.gomokult.exception.UserRegisteredException;
import com.gomokult.service.UserService;

/**
 * 提供与账户系统相关的服务实现。
 * @author 赵哲群
 */
public class UserServiceImpl implements UserService {

	private static final UserServiceImpl singleton = new UserServiceImpl();
	public static UserServiceImpl getSingleton() { return singleton; }
    private UserDAO userDAO = UserDAOImpl.getSingleton();      //注入UserDAO

    @Override
    public int registerUser(String email, String nickname, String password) throws UserRegisteredException {
        if (userDAO.insertrUser(email, nickname, password) == 0)
            throw new UserRegisteredException();
        return userDAO.insertrUser(email, nickname, password);
    }

    @Override
    public int loginVerify(String email, String password) {
        return userDAO.selectUser(email, password);
    }

    @Override
    public User getUserInfo(int userID) throws InvalidIDException {
        if (userDAO.selectUserbyID(userID) == null)
            throw new InvalidIDException();
        return userDAO.selectUserbyID(userID);
    }

    @Override
    public User getUserInfo(String email) throws InvalidIDException {
        return null;
    }

    @Override
    public void setUserInfo(int executorID, User pattern) throws InvalidIDException, IllegalOperationException, UserRegisteredException {
        userDAO.updateUser(executorID, pattern);
    }
}
